package com.stylefeng.guns.modular.vpn.utils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.system.dao.VpnBusinessDataInfoMapper;
import com.stylefeng.guns.modular.system.dao.VpnNewUserMapper;
import com.stylefeng.guns.modular.system.dao.VpnTimerStatisticsMapper;
import com.stylefeng.guns.modular.vpn.entity.NewUserInfoEntity;
import com.stylefeng.guns.modular.vpn.entity.RetentionUserInfo;
import com.stylefeng.guns.modular.vpn.entity.StatisticsFinancialInfoEntity;
import com.stylefeng.guns.modular.vpn.entity.StatisticsFinancialPackageInfoEntity;
import com.stylefeng.guns.modular.vpn_business_info.service.IVpnBusinessDataInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description 业务数据，定时任务用到的数据统计方法
 * @Author Mr.God
 * @Date 2020-03-11 20:40
 **/
@Service
public class BusinessTaskUtil {

    @Autowired
    private VpnTimerStatisticsMapper statisticsMapper;

    @Autowired
    private VpnNewUserMapper newUserMapper;

    @Autowired
    private VpnBusinessDataInfoMapper businessDataInfoMapper;

    /**
     * 统计每天注册用户信息到拉新数据表中
     */
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    @Transactional
    public void statisticsUserInfo() throws Exception{
//        EntityWrapper<NewUserInfoEntity> entityWrapper = new EntityWrapper();
//        entityWrapper.orderBy("id",false);

        NewUserInfoEntity lastInfo = newUserMapper.queryLastNewUserInfo();

        String lastDate = null;
        int downloadCount = 0;
        int regCount = 0;
        if(lastInfo != null){
            lastDate = TimeUtil.dateToGTMString(lastInfo.getQueryTime(),"yyyy-MM-dd");
            regCount = lastInfo.getTotalRegCount();
            downloadCount = lastInfo.getTotalDownloadCount();
        }

        List<NewUserInfoEntity> userInfoEntities =
                newUserMapper.queryNewUserInfo(lastDate);
        if(userInfoEntities == null || userInfoEntities.size() == 0){
            return;
        }
        int size = userInfoEntities.size();

        for (int i = 0;i<size;i++){
            NewUserInfoEntity entity = userInfoEntities.get(i);
            if(entity.getTodayDownloadCount() != null){
                downloadCount += entity.getTodayDownloadCount();
            }
            entity.setTotalDownloadCount(downloadCount);
            if(entity.getTodayRegCount() != null){
                regCount += entity.getTodayRegCount();
            }
            entity.setTotalRegCount(regCount);
        }

        statisticsMapper.saveStatisticsUserInfo(userInfoEntities);
    }

    /**
     * 统计每天注册用户信息到财务数据表中
     */
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    @Transactional
    public void statisticsFinancialInfo() throws Exception{
        StatisticsFinancialInfoEntity lastInfo =
                statisticsMapper.queryLastFinancialInfo();

        double totalAmount = 0;//累计金额
        Integer totalNonPaymentCount = 0;//累计未支付总 数量
        Integer totalPaymentCount = 0;//累计支付总数量
        String lastDate = null;
        if(lastInfo != null){
            totalAmount = lastInfo.getTotalAmount();
            totalNonPaymentCount = lastInfo.getTotalNonPaymentCount();
            totalPaymentCount = lastInfo.getTotalPaymentCount();
            lastDate = TimeUtil.dateToGTMString(lastInfo.getQueryTime(),"yyyy-MM-dd");
        }

        List<StatisticsFinancialInfoEntity> entityList =
                statisticsMapper.statisticsFinancialInfo(lastDate);
        if(entityList == null || entityList.size() == 0){
            return;
        }
        int size = entityList.size();
        for (int i=0; i < size; i++){
            StatisticsFinancialInfoEntity entity = entityList.get(i);

            //计算当日未购买人数
            int todayNoPayCount = entity.getTodayRegCount() - entity.getTodayPaymentCount();
            if(todayNoPayCount > 0){
                entity.setTodayNonPaymentCount(todayNoPayCount);
                totalNonPaymentCount += todayNoPayCount;
            }else{
                entity.setTodayNonPaymentCount(0);
            }

            //计算累计未购买总数量
            entity.setTotalNonPaymentCount(totalNonPaymentCount);

            //计算累计购买总金额
            if(entity.getTodayTotalAmount() != null){
                totalAmount += entity.getTodayTotalAmount();
            }
            entity.setTotalAmount(totalAmount);

            //计算累计购买总数量
            if (entity.getTodayPaymentCount() > 0){
                totalPaymentCount += entity.getTodayPaymentCount();
            }
            entity.setTotalPaymentCount(totalPaymentCount);

            //累计总付费率 总支付人数/总注册人数
            entity.setTotalPaymentRatio((double) entity.getTotalPaymentCount() / (entity.getTotalPaymentCount() + entity.getTotalNonPaymentCount()));
        }

        statisticsMapper.saveStatisticsFinancialInfo(entityList);
    }

    /**
     * 统计每天套餐数据到财务数据表中
     */
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    @Transactional
    public void statisticsFinancialPackageInfo() throws Exception{
        StatisticsFinancialPackageInfoEntity lastInfo =
                statisticsMapper.queryLastFinancialPackageInfo();
        int total0 = 0;
        int total1 = 0;
        int total2 = 0;
        int total3 = 0;
        int total4 = 0;
        String lastDate = null;
        if(lastInfo != null){
            total0 = lastInfo.getBuyP0TotalCount();
            total1 = lastInfo.getBuyP1TotalCount();
            total2 = lastInfo.getBuyP2TotalCount();
            total3 = lastInfo.getBuyP3TotalCount();
            total4 = lastInfo.getBuyP4TotalCount();
            lastDate = TimeUtil.dateToGTMString(lastInfo.getQueryTime(),"yyyy-MM-dd");
        }

        List<StatisticsFinancialPackageInfoEntity> entityList =
                statisticsMapper.statisticsFinancialPackageInfo(lastDate);
        if(entityList == null || entityList.size() == 0){
            return;
        }

        int size = entityList.size();

        for (int i = 0;i<size;i++){
            StatisticsFinancialPackageInfoEntity entity = entityList.get(i);
            if(entity.getTodayBuyP0Count() != null){
                total0 += entity.getTodayBuyP0Count();
            }
            entity.setBuyP0TotalCount(total0);

            if(entity.getTodayBuyP1Count() != null){
                total1 += entity.getTodayBuyP1Count();
            }
            entity.setBuyP1TotalCount(total1);

            if(entity.getTodayBuyP2Count() != null){
                total2 += entity.getTodayBuyP2Count();
            }
            entity.setBuyP2TotalCount(total2);

            if(entity.getTodayBuyP3Count() != null){
                total3 += entity.getTodayBuyP3Count();
            }
            entity.setBuyP3TotalCount(total3);

            if(entity.getTodayBuyP4Count() != null){
                total4 += entity.getTodayBuyP4Count();
            }

            entity.setBuyP4TotalCount(total4);
        }

        statisticsMapper.saveStatisticsFinancialPackageInfo(entityList);
    }

    /**
     * 统计活跃用户数据信息
     */
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    @Transactional
    public void statisticsRetentionUserInfo() throws Exception{
        RetentionUserInfo lastInfo = businessDataInfoMapper.findLastRetentionUserInfo();
        String lastDate = null;
        if(lastInfo != null){
            lastDate = TimeUtil.dateToGTMString(lastInfo.getQueryTime(),"yyyy-MM-dd");
        }

        List<RetentionUserInfo> retentionUserInfos =
                statisticsMapper.statisticsRetentionUserInfo(lastDate);
        if(retentionUserInfos == null || retentionUserInfos.size()  == 0){
            return;
        }
        statisticsMapper.saveRetentionUserInfo(retentionUserInfos);
    }

}
